import type { ToolInstallVersionItem } from "@/pages/tools/common/types";
import { requestGet } from "@/framework/util/http";
import type { NpmMirrorFileItem } from "@/pages/tools/common/npmmirror-types";
import { fetchMirrorWebsite } from "@/spider/ipc";

export async function getPythonVersionListFromNpmmirror(): Promise<ToolInstallVersionItem[]> {
  try {
    const resp = await requestGet<NpmMirrorFileItem[]>("https://registry.npmmirror.com/-/binary/python/");
    const list = resp.data
      .filter(it => it.type === "dir" && /^[3-9]\..*\/$/.test(it.name))
      .map<ToolInstallVersionItem>(it => {
        const version = it.name.substring(0, it.name.length - 1);
        const modified = new Date(it.modified);
        return { id: it.id, name: version, version, url: it.url, modified };
      });
    list.sort((a, b) => b.modified!.getTime() - a.modified!.getTime());
    // list.sort((a, b) => semver.compare(b.version, a.version));
    return list;
  } catch (e: any) {
    console.error("getPythonDownloadList error", e);
    return [];
  }
}

export async function getPythonDownloadFilesFromNpmmirror(version: string): Promise<ToolInstallVersionItem[]> {
  const resp = await requestGet<NpmMirrorFileItem[]>(`https://registry.npmmirror.com/-/binary/python/${version}/`);
  return resp.data
    .filter(it => it.type === "file" && it.name.endsWith("-amd64.exe"))
    .map<ToolInstallVersionItem>(it => ({
      id: it.id,
      name: it.name,
      url: it.url,
      version: version,
      modified: !!it.modified ? new Date(it.modified) : undefined,
    }));
}

export function getPythonVersionListFromHuaweiCloud(): Promise<ToolInstallVersionItem[]> {
  return getVersionListFromWebsite("https://mirrors.huaweicloud.com/python/");
}

export function getPythonVersionListFromOfficial(): Promise<ToolInstallVersionItem[]> {
  return getVersionListFromWebsite("https://www.python.org/ftp/python/");
}

async function getVersionListFromWebsite(url: string) {
  try {
    const res = await fetchMirrorWebsite(url);
    const list = res
      .filter(it => /^[3-9]\..*$/.test(it.name))
      .map<ToolInstallVersionItem>(it => {
        const version = it.name;
        const modified = !!it.lastModified ? new Date(it.lastModified) : undefined;
        return { id: `py-${version}`, name: version, version, url: url + it.name + "/", modified };
      });
    list.sort((a, b) => (b.modified?.getTime() ?? 0) - (a.modified?.getTime() ?? 0));
    // list.sort((a, b) => semver.compare(b.version, a.version));
    return list;
  } catch (e: any) {
    console.error(e);
    return [];
  }
}

export function getPythonDownloadFilesFromHuaweiCloud(version: string): Promise<ToolInstallVersionItem[]> {
  return getDownloadFilesFromWebsite(`https://mirrors.huaweicloud.com/python/${version}/`, version);
}

export function getPythonDownloadFilesFromOfficial(version: string): Promise<ToolInstallVersionItem[]> {
  return getDownloadFilesFromWebsite(`https://www.python.org/ftp/python/${version}/`, version);
}

async function getDownloadFilesFromWebsite(url: string, version: string): Promise<ToolInstallVersionItem[]> {
  const res = await fetchMirrorWebsite(url);
  return res
    .filter(it => it.name.endsWith("-amd64.exe"))
    .map<ToolInstallVersionItem>(it => ({
      id: `py-${version}`,
      name: it.name,
      url: url + it.name,
      version,
      modified: !!it.lastModified ? new Date(it.lastModified) : undefined,
    }));
}
